<template>
  <div class="xtx-form">
    =={{ openId }}
    <div class="user-info">
      <!-- <img
        src="http://qzapp.qlogo.cn/qzapp/101941968/57C7969540F9D3532451374AA127EE5B/50"
        alt=""
      /> -->
      <img :src="avatar" alt="" />
      <p>Hi，{{ nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="form.mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="form.code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" @click="btnSend">{{
          countTime == 0 ? "发送验证码" : countTime + "s后在获取"
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </div>
</template>

<script>
import QC from "qc";
import { reactive, ref } from "vue";
import { useCountDown } from "@/compositions";
import { message } from "@/components/Message";

import { reqUserQQBindCode, reqUserQQBindLogin } from "@/api/user";
export default {
  name: "CallbackBind",
  props: {
    openId: {
      type: String,
      default: "",
    },
  },
  setup(props) {
    /* 获取qq用户信息 */
    const nickname = ref("");
    const avatar = ref("");
    //  获取qq授权登录后的用户信息
    QC.api("get_user_info").success((res) => {
      console.log(res, 9911);
      nickname.value = res.data.nickname;
      avatar.value = res.data.figureurl_qq;
    });

    const form = reactive({
      mobile: "13012345753",
      code: "123456",
    });
    const { countTime, start, pause, resume } = useCountDown(60);
    const btnSend = async () => {
      // 防止连续点击
      if (countTime.value > 0) return;
      /*
      1-校验手机号
      2-发送请求获取验证码
      */
      var reg =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (!reg.test(form.mobile)) {
        message({ type: "error", text: "请输入合法的11位手机号" });
        return;
      }

      const res = await reqUserQQBindCode(form.mobile).catch((err) => {
        console.dir(err.response, 88);
        return message({ type: "error", text: err.response.data.message });
      });
      message({ type: "success", text: "手机号已发送" });
      start(); //  开启倒计时
    };

    const submit = async () => {
      //1-验证表单
      //2-发送请求
      var reg =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (!reg.test(form.mobile)) {
        message({ type: "error", text: "请输入合法的11位手机号" });
        return;
      }
      if (!form.code.trim()) {
        message({ type: "error", text: "请输入验证码" });
        return;
      }

      const res = await reqUserQQBindLogin({ ...form, unionId: props.openId });
    };
    return { nickname, avatar, btnSend, countTime, form, submit };
  },
};
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
